/**
 * Copyright © 2011 looty - Textoo
 * @link http://www.textoo.org
 * @link http://code.google.com/p/textoo/
 * @author lab9 - Bertrand Larrieu
 * @mail lab9.fr@gmail.com
 * @version 2.0
 */

package org.looty.core.textoo.render.glyph 
{
	import flash.events.*;
	import flash.text.*;
	import flash.utils.*;
	import org.looty.log.*;
	import org.looty.textoo.font.*;
	
	public class GlyphManager extends EventDispatcher
	{
		
		private var _tables								:Dictionary;
		private var _useDeviceFonts						:Boolean;
		
		
		
		public function GlyphManager() 
		{
			_tables = new Dictionary();
			_useDeviceFonts = true;
		}
		
		public function getTable(textFormat:TextFormat, useDeviceFonts:Boolean, isTextLightType:Boolean, sharpness:int = 0, thickness:int = 0):GlyphTable
		{
			var key:String = getKey(textFormat, useDeviceFonts, isTextLightType, sharpness, thickness);
			
			if (_tables[key] == undefined) _tables[key] = new GlyphTable(textFormat, FontUtils.getEmbeddedFont(textFormat), isTextLightType ? TextColorType.LIGHT_COLOR : TextColorType.DARK_COLOR,  _useDeviceFonts, sharpness, thickness);			
			return _tables[key];
		}
		
		public function clear(textFormat:TextFormat, useDeviceFonts:Boolean, isTextLightType:Boolean, sharpness:int = 0, thickness:int = 0):void
		{
			var key:String = getKey(textFormat, useDeviceFonts, isTextLightType, sharpness, thickness);			
			_tables[key] = null;
			delete _tables[key];
		}		
		
		private function getKey(textFormat:TextFormat, useDeviceFonts:Boolean, isTextLightType:Boolean, sharpness:int, thickness:int):String
		{
			return textFormat.font + "|" + (int(textFormat.italic) | (int(textFormat.bold) << 1) | (int(textFormat.underline) << 2) | (int(useDeviceFonts) << 3) | (int(isTextLightType) << 4)) + "#" + textFormat.size + "#" + sharpness + "/" + thickness;
		}	
		
		public function dump():void 
		{
			Looger.info(" -- fonts --");
			for each (var table:GlyphTable in _tables) Looger.info(" " + table.toString());
		}
		
	}
	
}